package com.yonyou.pmclouds.messagenotice.constructor.impl;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.constructiondiary.entity.ConstructionDiaryVO;
import com.yonyou.pmclouds.constructiondiary.rmiitf.ConstructionDiaryMaintain;
import com.yonyou.pmclouds.constructiondiary.rmiitf.ConstructionDiaryQuery;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
@Component
public class ConstructionDiaryNoticeConstructor extends AbstractMessageNoticeConstructor {

    private final ConstructionDiaryMaintain constructionDiaryMaintain;

    private final ProjectSimpleQuery projectSimpleQuery;

    private final ConstructionDiaryQuery constructionDiaryQuery;

    @Autowired
    public ConstructionDiaryNoticeConstructor(ConstructionDiaryMaintain constructionDiaryMaintain,
                                              ProjectSimpleQuery projectSimpleQuery,
                                              ConstructionDiaryQuery constructionDiaryQuery) {
        this.constructionDiaryMaintain = constructionDiaryMaintain;
        this.projectSimpleQuery = projectSimpleQuery;
        this.constructionDiaryQuery = constructionDiaryQuery;
    }

    @Override
    protected MessageNoticeVO[] generateContent(Entity vo) throws BusinessException {
        MessageNoticeVO noticeVO = coverBusinessVOToMessageVO(vo);
        if(noticeVO == null){
            return null;
        }
        ConstructionDiaryVO constructionDiaryVO = constructionDiaryQuery.queryDetailByID(noticeVO.getRelateKey());
        ProjectVO projectVO = projectSimpleQuery.queryProject(constructionDiaryVO.getPk_project());

        String prefix = getApprovalPrefixMessage(constructionDiaryVO.getBillstatus());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String dateStr = sdf.format(constructionDiaryVO.getCreationtime());

        String messageContent = APPROVAL_PREFIX_MESSAGE + "[" +dateStr+ "]的日志，"  + prefix;

        noticeVO.setMessageTitle(projectVO.getSname());
        noticeVO.setMessageReceiver(constructionDiaryVO.getCreator());
        noticeVO.setPkTenant(constructionDiaryVO.getPkTenant());
        noticeVO.setMessageContent(messageContent);
        noticeVO.setRelateType(constructionDiaryVO.getBill_type());

        return new MessageNoticeVO[]{noticeVO};
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.CONSTRUCTION_DIARY_BILLTYPE;
    }
}
